package com.lxd.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.lxd.redis.RedisComponent;
import org.springframework.stereotype.Service;

import com.lxd.entity.enums.PageSize;
import com.lxd.entity.query.NotificationQuery;
import com.lxd.entity.po.Notification;
import com.lxd.entity.vo.PaginationResultVO;
import com.lxd.entity.query.SimplePage;
import com.lxd.mappers.NotificationMapper;
import com.lxd.service.NotificationService;
import com.lxd.utils.StringTools;


/**
 *  业务接口实现
 */
@Service("notificationService")
public class NotificationServiceImpl implements NotificationService {

	@Resource
	private NotificationMapper<Notification, NotificationQuery> notificationMapper;
	@Resource
	private RedisComponent redisComponent;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<Notification> findListByParam(NotificationQuery param) {
		return this.notificationMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(NotificationQuery param) {
		return this.notificationMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<Notification> findListByPage(NotificationQuery param,String token) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE7.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		param.setOrderBy("is_read asc,created_at desc");
		//被通知人的userId
		Long userId = Long.valueOf(redisComponent.getTokenUserInfoDto(token).getUserId());
		param.setUserId(userId);
		List<Notification> list = this.findListByParam(param);
		PaginationResultVO<Notification> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(Notification bean) {
		return this.notificationMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<Notification> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.notificationMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<Notification> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.notificationMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(Notification bean, NotificationQuery param) {
		StringTools.checkParam(param);
		return this.notificationMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(NotificationQuery param) {
		StringTools.checkParam(param);
		return this.notificationMapper.deleteByParam(param);
	}

	/**
	 * 根据Id获取对象
	 */
	@Override
	public Notification getNotificationById(Long id) {
		return this.notificationMapper.selectById(id);
	}

	/**
	 * 根据Id修改
	 */
	@Override
	public Integer updateNotificationById(Notification bean, Long id) {
		return this.notificationMapper.updateById(bean, id);
	}

	/**
	 * 根据Id删除
	 */
	@Override
	public Integer deleteNotificationById(Long id) {
		return this.notificationMapper.deleteById(id);
	}
}